<?php 
namespace app\admin\Controller;
use think\Db;//导入数据库类 (DB类)
use think\Controller;
use think\Session;
use think\Request;
use app\admin\model\User;
class Links extends Admin

{
    
    public function lst()
    {

        if(input('get.a') == 1){
            db('link')->where('id',input('get.id'))->update(['status'=>0]);
        }else{
            db('link')->where('id',input('id'))->update(['status'=>1]);
        }
        $list = db('link')->select();
        $this->assign('data',$list);
        return view('links/admin-link');
    }
    public function ad()
    {
        return view('links/add');
    }

    public function add()
    {
        $validate = validate('Links');
        if(Request::instance()->isPost()){

            if(!$validate->check( input()) ){
                return $validate->getError();
            }else{
                 if(db('link')->insert(input())){
                    return 'ok';
                 }else{
                     return 'no';
                 }

            }
        }
    }

    public function del()
    {
        if(Request::instance()->isPost()){
            if(db('link')->delete(input('post.id'))){
                return '删除成功';
            }else{
                return '删除失败';
            }
        }
    }

    // 编辑
    public function edi()
    {
        $list = db('link')->where('id',input('id'))->select();
        $this->assign('data',$list);
        return view('links/edit');
    }
    // 执行修改
    public function edit()
    {
        $validate = validate('Links');
        if(Request::instance()->isPost()){

            if(!$validate->check( input()) ){
                return $this->error($validate->getError());
            }else{
                if(db('link')->where('id',input('id') )
                     ->update( [ 'linkname'  => input('post.linkname'),
                                 'interlink' => input('post.interlink'),
                                 'status'=>input('post.status')
                        ] )){

                    $this->success('修改成功','lst');
                }else{
                    $this->error('修改失败');
                }
            }
            
        }
    }

}